﻿/*
 * WebFlow Extension for ASP.NET MVC JavaScript Library
 * http://webflow.codeplex.com
 *
 * Copyright 2012, Sławomir Rosiek
 */

(function ($) {
    $.webflow = {
        action: function (form, action) {
            var button = $(form).find('input').filter('input[name="ActionName.' + action + '"]');
            if (button.length > 0) {
                button.click();
            }
            else {
                var actionHidden = $('<input type="hidden" name="ActionName" />').val(action);
                var actionButton = $('<input type="hidden" />').attr('name', 'ActionName.' + action);

                $(form).append(actionHidden).append(actionButton).submit();
            }
        }
    }

    $(function () {
        $('input,button').filter('*[data-val-ignore-rules=true]').click(function () {
            var validator = $(this).parents('form').data('validator');
            validator.cancelSubmit = true;
        });

        $('form[data-flow] *').keypress(function (e) {
            if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) {
                $('button,input').filter('[data-flow=default]').click();
                return false;
            } else {
                return true;
            }
        });
    });
})(jQuery);